<?php

require_once __DIR__.'/sql.php';

error_reporting(E_ALL & ~E_NOTICE);

$file = "/tmp/dou_sniffer";
$fp = fopen($file,"x");
if($fp){
    $pid = posix_getpid();
    fwrite($fp,$pid);
    fclose($fp);

    $sql_link = MySQL::getInstance();
    $res_sel = $sql_link->prepare('SELECT sniffer_data.id,sniffer_infos.room_id,sniffer_data.sniffer_id,sniffer_data.user_mac,
    sniffer_data.rssi,UNIX_TIMESTAMP(sniffer_data.time) AS time FROM sniffer_data,
    sniffer_infos WHERE sniffer_data.sniffer_id = sniffer_infos.sniffer_id');

    $res_del = $sql_link->prepare('DELETE FROM sniffer_data WHERE id <= :last_id');
    $res_del->bindParam(':last_id',$del_id);

    $last_time = time();
    $update_time = $last_time;
    $mac_data = array();
    $up_data = array();

    while(true){
        $res_sel->execute();
        $sel_data = $res_sel->fetchAll(PDO::FETCH_ASSOC);

        if(!empty($sel_data)){
            $last_time = time();

            //删除已取数据
            $del_id = end($sel_data)['id'];
            $res_del->execute();

            $user_sel = $sql_link->prepare('SELECT user_id,phone_mac FROM user_infos WHERE phone_mac=:mac OR pc_mac=:mac');
            $user_sel->bindParam(':mac',$user_mac);
            foreach($sel_data as $data){
                if($last_time - $data['time'] > 300){       //5分钟之前的抓包数据不处理
                    continue;
                }
                $user_mac = $data['user_mac'];
                if( ! isset($mac_data[$user_mac] ) ){
                    $mac_data[$user_mac] = array();

                    $user_sel->execute();
                    $user_info = $user_sel->fetch(PDO::FETCH_ASSOC);

                    if(!empty($user_info)){
                        $mac_data[$user_mac]['user_id'] = $user_info['user_id'];
                        $mac_data[$user_mac]['phone'] = ($user_mac == $user_info['phone_mac'] );
                    }
                }
                $mac_data[$user_mac]['time'] = $data['time'];

                if(! isset($mac_data[$user_mac]['user_id'] )){  //无用户id不处理
                    continue;
                }

                if( ! isset($up_data[ $mac_data[$user_mac]['user_id'] ] ) ){
                    $up_data[ $mac_data[$user_mac]['user_id'] ] = array();
                    $up_data[ $mac_data[$user_mac]['user_id'] ] ['room_id'] = $data['room_id'];
                    $up_data[ $mac_data[$user_mac]['user_id'] ] ['start_time'] = $data['time'];
                }

                if($up_data[ $mac_data[$user_mac]['user_id'] ]['room_id'] != $data['room_id']){
                    if( $mac_data[$user_mac]['phone'] ){     //如果同时检测到同一用户手机和电脑在不同房价，以手机为准
                        $up_data[ $mac_data[$user_mac]['user_id'] ]['room_id'] = $data['room_id'];
                        $up_data[ $mac_data[$user_mac]['user_id'] ] ['start_time'] = $data['time'];
                        $up_data[ $mac_data[$user_mac]['user_id'] ] ['end_time'] = $data['time'];
                        if(isset($up_data[ $mac_data[$user_mac]['user_id'] ] ['data_id'])){
                            unset($up_data[ $mac_data[$user_mac]['user_id'] ] ['data_id']);
                        }
                    }
                }else{
                    $up_data[ $mac_data[$user_mac]['user_id'] ] ['end_time'] = $data['time'];
                }
                
            }
            $user_sel = null;
            unset($sel_data);
        }else if(time() - $last_time > 1200){       //超过20分钟没有新的抓包数据
            break;  //结束运行
        }

        if( time() - $update_time > 300){           //每5分钟更新下统计信息
            $update_time = time();

            foreach($mac_data as $key=>$data){
                $old_time = $update_time - $data['time'];

                if( $old_time > 1200){
                    unset($mac_data[$key]);
                }
            }

            $data_insert = $sql_link->prepare('INSERT INTO statistical_data (user_id,room_id,start_time,end_time) VALUES (
                :user_id,:room_id,:start_time,:end_time)');
            $data_insert->bindParam(':user_id',$sql_data['user_id']);
            $data_insert->bindParam(':room_id',$sql_data['room_id']);
            $data_insert->bindParam(':start_time',$sql_data['start_time']);
            $data_insert->bindParam(':end_time',$sql_data['end_time']);

            $data_update = $sql_link->prepare('UPDATE statistical_data SET end_time=:end_time WHERE data_id=:data_id');
            $data_update->bindParam(':end_time',$sql_data['end_time']);
            $data_update->bindParam(':data_id',$sql_data['data_id']);

            foreach($up_data as $key=>$data){
                $old_time = $update_time - $data['end_time'];
                $hold_time = $data['end_time'] - $data['start_time'];

                if( ($old_time > 300 && $hold_time <= 1200 ) || $old_time > 1200){
                    unset($up_data[$key]);
                }else if( $old_time < 320 &&  $hold_time >1200){

                    if(isset($data['data_id']) ){
                        //更新
                        $sql_data['end_time'] = date("Y-m-d H:i:s", $data['end_time']);
                        $sql_data['data_id'] = $data['data_id'];
                        $data_update->execute();
                    }else{
                        //插入
                        $sql_data['user_id'] = $key;
                        $sql_data['room_id'] = $data['room_id'];
                        $sql_data['start_time'] = date("Y-m-d H:i:s", $data['start_time']);
                        $sql_data['end_time'] = date("Y-m-d H:i:s", $data['end_time']);
                        $data_insert->execute();
                        $up_data[$key]['data_id'] = $sql_link->lastInsertId();
                    }
                }
            }
            unset($data_insert);
            unset($data_update);
            unset($sql_data);
        }

        sleep(3);
    }
    unlink($file);
}